package com.sky.service.impl;

import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.ShoppingCart;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.ShoppingCartMapper;
import com.sky.service.ShoppingCartService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class ShoppingCartServiceimpl implements ShoppingCartService {
    @Autowired
    private ShoppingCartMapper shoppingCartMapper;
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private SetmealMapper setmealMapper;

    @Override
    public void add(ShoppingCartDTO shoppingCartDTO) {
        //判断是否有该菜品
        ShoppingCart shoppingcart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO,shoppingcart);
        Long UserId = BaseContext.getCurrentId();
        shoppingcart.setUserId(UserId);

        List<ShoppingCart> listCart = shoppingCartMapper.list(shoppingcart);
        //如果有，数量加1
        if(listCart != null && listCart.size() > 0){
            ShoppingCart cart = listCart.get(0);
            cart.setNumber(cart.getNumber()+1);
            shoppingCartMapper.updataNumber(cart);
        }else {
            //没有再添加

            //要判断传来的是菜品还是套餐
            Long dishId = shoppingCartDTO.getDishId();
            if(dishId != null){
                //传来的是菜品
                shoppingcart.setNumber(1);
                shoppingcart.setCreateTime(LocalDateTime.now());//还差name,image,现在要根据dishI查name，iamge
                Dish dish = dishMapper.getbyId(dishId);
                shoppingcart.setName(dish.getName());
                shoppingcart.setImage(dish.getImage());
                shoppingcart.setAmount(dish.getPrice());

            } else {
                //传来的是套餐
                shoppingcart.setNumber(1);
                shoppingcart.setCreateTime(LocalDateTime.now());//还差name,image,现在要根据dishI查name，iamge
                Setmeal setmeal = setmealMapper.getbyId(shoppingCartDTO.getSetmealId());
                shoppingcart.setName(setmeal.getName());
                shoppingcart.setImage(setmeal.getImage());
                shoppingcart.setAmount(setmeal.getPrice());

            }
            shoppingCartMapper.insert(shoppingcart);
        }
    }

    @Override
    public List<ShoppingCart> listShoppingCart() {
        ShoppingCart shoppingCart = new ShoppingCart();
        Long currentId = BaseContext.getCurrentId();
        shoppingCart.setUserId(currentId);
        List<ShoppingCart> listCart = shoppingCartMapper.list(shoppingCart);

        return listCart;
    }

    @Override
    public void cleanshoppingCart() {
        Long currentId = BaseContext.getCurrentId();
        shoppingCartMapper.clean(currentId);
    }

    @Override
    public void sub(ShoppingCartDTO shoppingCartDTO) {
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO, shoppingCart);
        shoppingCart.setUserId(BaseContext.getCurrentId());
        List<ShoppingCart> ShoppingCarts = shoppingCartMapper.list(shoppingCart);
        shoppingCart = ShoppingCarts.get(0);

        if(shoppingCart.getNumber()>1){
            shoppingCart.setNumber(shoppingCart.getNumber()-1);
            shoppingCartMapper.updataNumber(shoppingCart);
        } else {
            shoppingCartMapper.deleteById(shoppingCart.getId());
        }

    }
}
